package com.dwin.dingnuo.mqtt;

import android.util.Log;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.greenrobot.eventbus.EventBus;

/**
 * 使用EventBus分发事件
 *
 * @author tq on 2021/12/07.
 */
public class MqttCallbackBus implements MqttCallback {
    private static final String TAG = "MqttManager";

    @Override
    public void connectionLost(Throwable cause) {
//        Logger.e(cause.getMessage());
        Log.d(TAG, "connectionLost: " + cause.getMessage());
        Log.d(TAG, "connectionLost: " + cause.getCause());
        Log.d(TAG, "connectionLost: " + cause.getLocalizedMessage());
        Log.d(TAG, "connectionLost: " + cause);
        // 通过eventBus发送消息给Activity，尝试重连mqtt服务器
        EventBus.getDefault().post(new DataSynEvent("connectionLost:"));
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) {
        Log.d(TAG, "messageArrived: " + message.toString());
        EventBus.getDefault().post(message);
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
    }

}
